<?php
namespace app\spadmin\controller\device;
use app\common\controller\Backend;
use think\Db;
class Device extends Backend{

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Device');
        $this->assign('place',Db::name('DevicePlace')->where('status',1)->column('id,place_title','id'));
        $this->assign('engine',Db::name('DeviceEngine')->where('status',1)->column('id,engine_name','id'));

    }

    public function index(){

        $this->request->filter(['strip_tags', 'trim']);
        if($this->request->isAjax()){
            $data = $this->request->param();
            if (isset($data['keywords']) && $data['keywords']) {
                $map['device_name'] = ['like', '%' . $data['keywords'] . '%'];
            }           
            
            $map['status'] = 1;         
            $list =   $this->model->where($map)->order('id desc')->paginate($data['limit'])->toArray();
            foreach ($list['data'] as $key => &$value) {
                if($value['place_id']){
                    $value['place'] = Db::name('DevicePlace')->where('id',$value['place_id'])->value('place_title');
                }else{
                    $value['place']='';
                }

                if($value['engine_id']){
                    $value['engine'] = Db::name('DeviceEngine')->where('id',$value['engine_id'])->value('engine_name');
                }else{
                    $value['engine']='';
                }
            }
            return $this->result($list);
        }
        return $this->view->fetch();
    }
    
}